package com.MarkToTop.main;

import com.MarkToTop.util.Constant;
import com.MarkToTop.util.GameUtil;
import com.MarkToTop.util.MusicUtil;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * MarkWang 版权所有 © Copyright 2020
 *
 * @CreateDate: Created in 2020-06-21 12:42
 * @Author: MarkToTop -(づ｡◕‿‿◕｡)づ
 * @Description:  小鸟的核心类，小鸟的飞行逻辑、绘制 在此类中控制
 */
public class Bird {

    //图片的总张数
    public static final int IMG_COUNT = 4;

    //图片资源数组对象
    private BufferedImage[] imgs;
    //小鸟的坐标属性
    public int x,y;

    //小鸟在Y轴速度的属性
    private int speedY;

    //小鸟飞的最大速度
    public static final int MAX_UP_SPEED = 10;
    public static final int MAX_DOWN_SPEED = 15;

    //定义鸟儿的飞行状态
    private int state;
    public static final int STATE_NORMAL = 0;
    public static final int STATE_UP = 1;
    public static final int STATE_DOWN = 2;
    public static final int STATE_DIEING = 3; //碰撞死亡过程
    public static final int STATE_DIED = 4;  //死亡落地

    //定义矩形碰撞
    private Rectangle rect;

    //游戏时间
    private GameTime gameTiming;

    //游戏结束的资源
    private BufferedImage over;
    private BufferedImage socre;
    private BufferedImage again;

    private int flash;

    //构造方法中进行资源初始化
    public Bird() {
        gameTiming = GameTime.getInstance();
        imgs = new BufferedImage[IMG_COUNT];
        for (int i = 0; i < IMG_COUNT; i++) {
            imgs[i] = GameUtil.loadBufferedImage(Constant.Birds_PATH[i]);
        }

        //小鸟的初始化坐标 -> 窗口的中心点?
        int w = imgs[0].getWidth();
        int h = imgs[0].getHeight();
        x = Constant.Frame_Width /3+imgs[0].getWidth();
        y = Constant.Frame_Height >> 1;
        int x = this.x - w/2;
        int y = this.y - h/2;
        rect = new Rectangle(x+2,y-2,w-4,h-4);
    }

    //绘制
    public void draw(Graphics g){
        flyLogic();
        //使用的图片资源的索引
        int index = state > STATE_DIEING ?STATE_DIEING :state;

        //小鸟图片的一半长度
        int halfBirdImgW = imgs[index].getWidth() >>1;
        int halfBirdImgH = imgs[index].getHeight() >>1;

        //绘制时注意图片的偏移
        g.drawImage(imgs[index],Constant.Frame_Width/3+halfBirdImgW,y-halfBirdImgH,null);

        if (state == STATE_DIED){
            drawGameOver(g);
        }else {
            drawTime(g);
        }
    }
    //绘制时间
    private void drawTime(Graphics g){
        g.setColor(Color.WHITE);
        g.setFont(Constant.TIME_FONT);
        g.drawString("飞行时间："+Float.toString(gameTiming.getTime()/1000.0f),40,80);
    }

    public void drawBagin(Graphics g){
        //使用的图片资源的索引
       state = STATE_NORMAL;
        //小鸟图片的一半长度
        int halfBirdImgW = imgs[state].getWidth() >>1;
        int halfBirdImgH = imgs[state].getHeight() >>1;
        g.drawImage(imgs[state],Constant.Frame_Width /2 -halfBirdImgW,y-halfBirdImgH,null);
    }

    /**
     * 绘制游戏结束和记分牌
     */
    private void drawGameOver(Graphics g){
        g.setColor(Color.WHITE);
        g.setFont(Constant.TIME_FONT);
        //绘制GameOver
        int x = Constant.Frame_Width-over.getWidth() >>1;
        int y = Constant.Frame_Height /3;
        g.drawImage(over,x,y,null);

        x = Constant.Frame_Width-socre.getWidth() >>1;
        y += over.getHeight() << 1;
        g.drawImage(socre,x,y,null);

        //绘制本局分数
        x = Constant.Frame_Width - socre.getWidth()/2 >> 1;
        y += socre.getHeight()>>1;

        String str = Long.toString(gameTiming.getSecondTime());
        x -= GameUtil.getStringWidth(Constant.TIME_FONT,str)>>1;
        y += GameUtil.getStringHeight(Constant.TIME_FONT,str);
        g.drawString(str,x,y);

        //绘制最高分数
        if (gameTiming.getBestTime() > 0){
            str = Long.toString(gameTiming.getBestTime());
            x += socre.getWidth()>>1;
            g.drawString(str,x,y);
        }
        //绘制继续游戏
        final int count = 10;
        if(flash ++ > count){
            x = (Constant.Frame_Width - again.getWidth() >>1) + 5;
            y = Constant.Frame_Height -250;
            g.drawImage(again,x,y,null);
            if(flash == count *2){
                flash = 0;
            }
        }

    }
    /**
     * 小鸟在Y坐标上的飞行逻辑部分
     */
    private void flyLogic(){
        switch (state){
            case STATE_NORMAL:
                break;
            case STATE_UP:
                speedY+= 2;
                if(speedY > MAX_UP_SPEED){
                    speedY = MAX_UP_SPEED;
                }
                y -= speedY;
                rect.y -=speedY; //碰撞层y运动
                //撞到上边缘时 (*右移 优先级 小于 +)
                if(y < (imgs[state].getHeight() >>1) + Constant.TOP_BAR_HEIGHT){
                    dying();
                }
                break;
            case STATE_DOWN:
                speedY +=2;
                if(speedY > MAX_DOWN_SPEED){
                    speedY = MAX_DOWN_SPEED;
                }
                y += speedY;
                rect.y +=speedY; //碰撞层y运动
                //撞到下边缘时
                if(y > Constant.Frame_Height - (imgs[state].getHeight() >>1)){
                    dying();
                }
                break;
            case STATE_DIEING:  //鸟死亡时自由落体
                speedY +=2;
                if(speedY > MAX_DOWN_SPEED){
                    speedY = MAX_DOWN_SPEED;
                }
                y += speedY;
                if(y > Constant.Frame_Height - (imgs[state].getHeight() >>1)){
                    y = Constant.Frame_Height - (imgs[state].getHeight() >>1);
                    //落在地上
                    died();
                }
                break;
            case STATE_DIED:  //鸟落地后
                break;
        }
    }

    /**
     * 改变鸟的飞翔状态，向上飞
     */
    public void fly(){
        if(state == STATE_DIEING || state == STATE_DIED ||state == STATE_UP)
            return;

            state = STATE_UP;
            speedY = 0;
        MusicUtil.playFly();  //播放飞翔音效
    }

    public void startTiming(){
        //开始计时
        if(gameTiming.isReady()){
            gameTiming.startTimeing();
        }
    }

    public void down() {
        if(state == STATE_DIEING||state == STATE_DIED || state == STATE_DOWN)
            return;

            state = STATE_DOWN;
            speedY = 0;
    }

    public Rectangle getRect() {
        return rect;
    }

    /**
     * 小鸟死亡
     */
    public void dying(){
        MusicUtil.playCrash();  //播放撞击音效
        state = STATE_DIEING;
        //结束计时
        gameTiming.endTiming();
        //加载游戏结束资源
        if (over == null){
            over = GameUtil.loadBufferedImage(Constant.OVER_IMG_PATH);
            socre = GameUtil.loadBufferedImage(Constant.SOCRE_IMG_PATH);
            again = GameUtil.loadBufferedImage(Constant.AGAIN_IMG_PATH);
        }
    }

    public void died(){
        state = STATE_DIED;
        GameFrame.setGameState(GameFrame.STATE_OVER);

    }
    public boolean isDie(){
        return state == STATE_DIED || state == STATE_DIEING;
    }

    /**
     * 当小鸟死亡后，进行重置
     */
    public void reset(){
        state = STATE_NORMAL;
        speedY = 0;
        x = Constant.Frame_Width >> 1;
        y = Constant.Frame_Height >> 1;

        int h = imgs[0].getHeight();
        rect.y = this.y - h/2 + 2;
        gameTiming.reset();
        flash = 0;
    }
}